home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: Question about sscanf
- Date: 8 Feb 1996 22:10 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <8FEB199622105557@erich.triumf.ca>
- References: <4fe1oq$kuf@zippy.cais.net>
- NNTP-Posting-Host: erich.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <4fe1oq$kuf@zippy.cais.net>, usaid@cais.cais.com (USAID) writes...
- >Here is a fragment of code that I'm having a problem with. The program
- >is bombing on the line containing the call to sscanf. It assigns to the
- >first variable fine, but blows up on the second one. I have no idea
- >what's wrong.
- >
- >int formatFile(void)
- >{
- ....
- > char *name;
- > char *value;
- ...
- > /* store the name and value in variables */
- > if (sscanf(line, "%s %s", name, value) != 2)
-
- name and value are uninitialized pointers - they may be pointing anywhere, so
- sscanf() will write the name and value strings in some random location.
-
- You must _never_ use a pointer without first making sure it points to a
- suitable memory space.
-
- Either make name and value arrays of suitable size, thus:
- char name[80];
- char value[20]; /* or whatever size is suitable... */
- or use malloc() to allocate space for them to point to.
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
-
-